package com.zjjc.QZEnergy.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtils {

    public static Calendar getFirstDayOfLastMonth(){    //返回上个月1号的00:00:00
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();

        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH,month - 1);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        System.out.println("上月起始时间为：" + sf.format(calendar.getTime()));
        return calendar;
    }

    public static Calendar getLastDayOfLastMonth(){    //返回上个月最后一天的23:59:59
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();

        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH,month - 1);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        System.out.println("上月截止时间为：" + sf.format(calendar.getTime()));

        return calendar;
    }

    public static Calendar getFirstSecondOfLastDay(){    //返回昨天的00:00:00
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,-1);
        calendar.set(Calendar.HOUR,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        System.out.println("昨日起始时间为：" + sf.format(calendar.getTime()));
        return calendar;
    }

    public static Calendar getLastSecondOfLastDay(){    //返回昨天的23:59:59
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,-1);
        calendar.set(Calendar.HOUR,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        System.out.println("昨日截止时间为：" + sf.format(calendar.getTime()));
        return calendar;
    }
}
